﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Reactive;
using System.Collections.Generic;
using System.Net.NetworkInformation;

namespace RssReaderTemplate.Model
{
    public class NetworkMonitor : IObservable<bool>
    {
        IObservable<bool> _monitor;

        public NetworkMonitor()
        {
            _monitor = Observable.FromEvent<EventArgs>(
                x => { NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(x); },
                x => { NetworkChange.NetworkAddressChanged -= new NetworkAddressChangedEventHandler(x); })
            .Select(x => NetworkInterface.GetIsNetworkAvailable())
            .StartWith(NetworkInterface.GetIsNetworkAvailable());
        }

        public IDisposable Subscribe(IObserver<bool> observer)
        {
            return _monitor.Subscribe(observer);
        }
    }
}
